package com.skycode5.dao;

import com.skycode5.bean.NewsBean;
import com.skycode5.util.DBConnection;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class NewsDao2 {
    DBConnection DBConn = new DBConnection();

    // todo 后期添加分页功能
    public List<NewsBean> getLIst() {
        Connection conn = DBConn.getConn();
        String sql = "select * from news";
        List<NewsBean> list = new ArrayList<NewsBean>();
        list = null;
        try {
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                NewsBean newsBean = new NewsBean();
                newsBean.setNewsId(rs.getInt("NewsID"));
                newsBean.setNewsTitle(rs.getString("NewsTitle"));
                newsBean.setNewsContent(rs.getString("NewsContent"));
                newsBean.setNewsTime(rs.getString("NewsTime"));
                newsBean.setAdmianName(rs.getString("AdminName"));
                list.add(newsBean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;

    }

    /**
     * @param news
     * @return
     */
    public int add(NewsBean news) {
        Connection conn = DBConn.getConn();
        String sql = "insert into news values(null,?,?,?,?)";
        try {
            PreparedStatement pmSt = conn.prepareStatement(sql);
            pmSt.setString(1, news.getNewsTitle());
            pmSt.setString(2, news.getNewsContent());
            pmSt.setString(3, (new java.util.Date()).toString());
            pmSt.setString(4, news.getAdmianName());
            return pmSt.executeUpdate();//执行添加 返回受影响的行数
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;//添加失败返回
    }

    public int update(NewsBean news) {
        Connection conn = DBConn.getConn();
        String sql = "update news set NewsTitle NewsConten=?, NewsTime=?,AdminName=? where NewsID=?";
        try {
            PreparedStatement pmSt = conn.prepareStatement(sql);
            pmSt.setString(1, news.getNewsTitle());
            pmSt.setString(2, news.getNewsContent());
            pmSt.setString(3, (new Date()).toString());
            pmSt.setString(4, news.getAdmianName());
            pmSt.setInt(5, news.getNewsId());
            return pmSt.executeUpdate();//执行添加 返回受影响的行数
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;//添加失败返回
    }

    public int delete(int newsID) {
        Connection conn = DBConn.getConn();
        try {
            String sql = "delete from news where NewsID=?";
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setInt(1, newsID);
            return pmst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
